package com.example.javaweb;

import java.util.HashMap;
import java.util.Map;

public class User {
    private String username;
    private String password1;
    private String password2;
    private String email;

    public Map<String, String> getErrors() {
        return errors;
    }

    private Map<String,String> errors = new HashMap<>();
    public User(String username, String password) {
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword1() {
        return password1;
    }

    public void setPassword1(String password1) {
        this.password1 = password1;
    }

    public String getPassword2() {
        return password2;
    }

    public void setPassword2(String password2) {
        this.password2 = password2;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public User(String username, String password1, String password2, String email) {
        this.username = username;
        this.password1 = password1;
        this.password2 = password2;
        this.email = email;
    }
    //创建验证方法
    public boolean yanzheng(){
        boolean flag=true;
        //判断用户名是否合法
        if(username == null|| username.trim().equals("")){
            errors.put("username","请输入用户名");
            flag=false;
        }
        //判断密码
        if(password1==null||password1.trim().equals("")){
            errors.put("password1","请输入密码");
            flag = false;
        }else if(password1.length()>12||password1.length()<6){
            errors.put("password1","请输入6~12个字符");
            flag = false;
        }
        if(password1!=null&&!password2.equals(password1)){
            errors.put("password2","两次密码输入不一致");
            flag = false;
        }
        //判断邮箱格式
        if(email==null||email.trim().equals("")){
            errors.put("email","请输入邮箱");
            flag = false;
        }else if(!email.matches("[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9]+$)")){
            errors.put("email","邮箱格式错误");
            flag = false;
        }



        return flag;
    }
}
